#include "dict.h"
#include <stdio.h>



Dict :: Dict (FILE * main_dict)
{
   char current_word [MAX_STRING_SIZE];
   string temp_word;
   while(!feof(main_dict))
   {
      fgets(current_word, MAX_STRING_SIZE, main_dict);
      temp_word.clear();
      size_t i = 0;
      for(i=0; (current_word[i] != '/')&&(current_word[i] != '\t')&&(current_word[i] != '\n')&&(current_word[i] != '\0'); ++i)
      {
         temp_word.push_back(current_word[i]);
      }
      data_.push_back(temp_word);
      data_set_.insert(temp_word);
   }
}


bool Dict:: addToDict (vector<string> words)
{
    for(size_t i = 0; i< words.size(); ++i)
    {
        data_.insert(data_.begin(),words[i]);
    }
    /*for(int i = 0; i<20; ++i)
    {
        printf("%s\n", data_[i].data());
    }*/
}


bool Dict :: findInSet (const string & find_word)
{
	if (data_set_.find(find_word) != data_set_.end())
		return true;
	else
		return false;
}

vector<string>  Dict :: find (const string & identifier,Comporator & cmp )
{
   size_t min_distance = 1000;
   vector <string> candidates;
   if(data_set_.find(identifier) != data_set_.end())
   {
      return candidates;
   }
   for (size_t i = 0; (i < data_.size())&&min_distance; ++i)
   {
      if(cmp.compare(identifier, data_[i]) < min_distance)
      {
         min_distance = cmp.compare(identifier, data_[i]);
         candidates.clear();
         candidates.push_back(data_[i]);
      }
      else
         if(cmp.compare(identifier, data_[i]) == min_distance)
         {
            candidates.push_back(data_[i]);
         }
   }
   if(!min_distance)
      candidates.clear();
   return candidates;
}

